package com.ruoyi.life.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.life.domain.vo.EnterpriseVo;
import com.ruoyi.life.mapper.ZsCustomerMapper;
import com.ruoyi.life.service.IZsCustomerService;
import com.ruoyi.omp.api.domain.ZsCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户信息Service业务层处理
 *
 * @author cjie
 * @date 2021-08-03
 */
@Service
public class ZsCustomerServiceImpl implements IZsCustomerService {
    @Autowired
    private ZsCustomerMapper zsCustomerMapper;

    /**
     * 查询客户信息
     *
     * @param id 客户信息ID
     * @return 客户信息
     */
    @Override
    public ZsCustomer selectZsCustomerById(String id) {
        return zsCustomerMapper.selectZsCustomerById(id);
    }

    /**
     * 查询客户信息列表
     *
     * @param zsCustomer 客户信息
     * @return 客户信息
     */
    @Override
    public List<ZsCustomer> selectZsCustomerList(ZsCustomer zsCustomer) {
        return zsCustomerMapper.selectZsCustomerList(zsCustomer);
    }

    /**
     * 新增客户信息
     *
     * @param zsCustomer 客户信息
     * @return 结果
     */
    @Override
    public int insertZsCustomer(ZsCustomer zsCustomer) {
        if (StringUtils.isEmpty(zsCustomer.getId())){
            zsCustomer.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        }
        zsCustomer.setCreateTime(DateUtils.getNowDate());
        zsCustomer.setCreateBy(SecurityUtils.getUsername());
        return zsCustomerMapper.insertZsCustomer(zsCustomer);
    }

    /**
     * 修改客户信息
     *
     * @param zsCustomer 客户信息
     * @return 结果
     */
    @Override
    public int updateZsCustomer(ZsCustomer zsCustomer) {
        zsCustomer.setUpdateTime(DateUtils.getNowDate());
        zsCustomer.setUpdateBy(SecurityUtils.getUsername());
        return zsCustomerMapper.updateZsCustomer(zsCustomer);
    }

    /**
     * 批量删除客户信息
     *
     * @param ids 需要删除的客户信息ID
     * @return 结果
     */
    @Override
    public int deleteZsCustomerByIds(String[] ids) {
        return zsCustomerMapper.deleteZsCustomerByIds(ids);
    }

    /**
     * 删除客户信息信息
     *
     * @param id 客户信息ID
     * @return 结果
     */
    @Override
    public int deleteZsCustomerById(String id) {
        return zsCustomerMapper.deleteZsCustomerById(id);
    }

    @Override
    public int insertEnterprise(EnterpriseVo zsCustomer, String cusId) {

        Map<String, String> params = new HashMap<>();
        params.put("id", zsCustomer.getId());
        params.put("zsId", cusId);
        params.put("name", zsCustomer.getEnterpriseName());
        params.put("person", zsCustomer.getEnterpriseContact());
        params.put("phone", zsCustomer.getEnterpriseContact());
        return   zsCustomerMapper.insertEnterprise(params);
    }

}
